import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSObject;

public class SoftClayMaker extends Script implements PaintListener {
	public long startTime = System.currentTimeMillis();
	public long time = System.currentTimeMillis();
	int waterFilled[] = { 1937, 1929 };
	int NotwaterFilled[] = { 1925, 1935 };
	int BankID = 36831;
	int HardClay = 434;
	int count = 0;
	int SoftClay = 1761;
	int amount;

	Color BG = new Color(0, 0, 0, 125);

	void AfterChecking() {
		if (getInventoryCount(waterFilled) == 14
				&& getInventoryCount(HardClay) == 14
				&& findObject(BankID) != null) {
			Watering();
			count = count + 14;
			wait(random(10000, 14000));
		} else {
			Bank();
		}
	}

	void Bank() {
		final RSObject Banks = findObject(BankID);
		atObject(Banks, "quickly");
		wait(random(250, 1000));
		if (RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()) {
			// bank.depositAll();
			if (!(getInventoryCount() == 0)) {
				clickMouse(395 + random(-12, 12), 309 + random(-9, 9), true);

				wait(random(600, 1000));
				if (!(getInventoryCount() == 0)) {
					bank.depositAll();
				}
			}
			wait(random(300, 600));

			WithDraw1();
			wait(random(250, 750));
			WithDraw2();
			wait(random(500, 1000));
			AfterChecking();
		} else {
			atObject(Banks, "quickly");
		}
	}

	@Override
	public String getAuthor() {
		return "Killa with Af Fix";
	}

	@Override
	public String getName() {
		return "SoftClayMaker";
	}

	@Override
	public String getScriptCategory() {
		return "Crafting";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<head>";
		html += "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>";
		html += "</head>";
		html += "<body>";
		html += "<center>";
		html += "<b><font size=\"6\" color=\"red\">" + getName() + " v"
				+ getVersion() + " c" + "</font></b><br>";
		html += "<font size=\"3\" color=\"red\">Please start in a bank & get id of booth.<br></font>";
		html += "</center>";
		html += "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">";
		html += "<tr><td><b>Bank Booth ID:</b>&nbsp<input type=\"text\" name=\"Bank1\" size=\"4\"></center></td></tr>";
		html += "<tr><td><b>Amount of clay:</b>&nbsp<input type=\"text\" name=\"amount\" size=\"4\"><center><b></center></td></tr>";
		html += "<tr><td colspan=2><b><font size=\"3\">&nbsp&nbsp Enjoy!</font></b></td><td  colspan=\"2\" align=right>"
				+ getVersion() + " &nbsp&nbsp</td></tr></table>";
		html += "</center></body>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() {
		return 1.01;
	}

	@Override
	public int loop() {
		if (getInventoryCount(waterFilled) == 14
				&& getInventoryCount(HardClay) == 14) {
			AfterChecking();
		}
		if (getInventoryCount(NotwaterFilled) == 14
				&& getInventoryCount(SoftClay) == 14
				&& findObject(BankID) != null) {
			Bank();
		}
		if (RSInterface.getInterface(109).isValid()) {
			final int x = 426, y = 77;
			clickMouse(x + random(2, -3), y + random(2, -3), true);
		}
		if (getInventoryCount() == 0 && findObject(BankID) != null) {
			Bank();
		}
		return random(5, 10);
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			g.setColor(BG);
			g.fill3DRect(9, 210, 170, 120, true);
			g.setColor(Color.white);
			g.drawString("SoftClayMaker by killa", 18, 230);
			g.drawString("Watered: " + count, 18, 250);
			g.drawString("Running for: " + hours + "h " + minutes + "m "
					+ seconds + "s", 18, 270);
			g.drawString("Enjoy The Script!!!!", 18, 290);
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		try {
			log(args.get("amount"));
			amount = Integer.parseInt(args.get("amount"));
		} catch (final Exception e) {
			log("Invalid Amount of Clay");
			return false;
		}
		try {
			log(args.get("Bank1"));
			BankID = Integer.parseInt(args.get("Bank1"));
		} catch (final Exception e) {
			log("Invalid Bank Booth");
			return false;
		}
		return true;
	}

	public boolean useItem2(final RSItem item, final RSItem targetItem) {
		if (getCurrentTab() != Constants.TAB_INVENTORY) {
			openTab(Constants.TAB_INVENTORY);
		}
		if (random(0, 10) == 4) {
			atInventoryItem(item.getID(), "Use");
			return atInventoryItem(targetItem.getID(), "Use");
		} else {
			atInventoryItem(item.getID(), "");
			return atInventoryItem(targetItem.getID(), "");
		}

	}

	void Watering() {

		useItem2(getInventoryItemByID(HardClay),
				getInventoryItemByID(waterFilled));
		wait(random(1000, 1200));
		final int x2 = 256, y2 = 416;
		clickMouse(x2 + random(-66, 66), y2 + random(-28, 28), false);
		if (random(0, 12) == 3) {
			atMenu("X");
			wait(random(700, 900));
			final String temp = "" + 14;
			sendText(temp, true);
		} else {
			atMenu("All");
		}

	}

	void WithDraw1() {
		final String temp = "" + 14;
		final int x = random(38, 73), y = random(92, 123);
		clickMouse(x, y, false);
		if (!atMenu("14")) {
			atMenu("X");
			wait(random(1000, 2000));
			sendText(temp, true);
		} else {
			atMenu("14");
		}
	}

	void WithDraw2() {
		wait(random(1000, 2000));
		final String temp = "" + 14;
		final int x = random(88, 96), y = random(100, 107);
		clickMouse(x, y, false);

		if (!atMenu("14")) {
			atMenu("X");
			wait(random(1000, 2000));
			sendText(temp, true);
		} else {
			atMenu("14");
		}

		if (getInventoryCount(waterFilled) == 14) {
			WithDraw2();
		}
		wait(random(300, 700));
		bank.close();
	}
}